package zy;
/*
 *@Student 张策
 */
import java.util.*;

public class Warehouse {
    //开局1W元做生意
    public Double money=10000.0;
    public List<Dish> dishes = new ArrayList<>();
    public List<Cust> custs = new ArrayList<>();
    public static List<Order> orders = new ArrayList<>();

    //*************************************************
    Scanner scanner=new Scanner(System.in);

    public List<Cust> getCusts() {
        return custs;
    }

    public void setCusts(List<Cust> custs) {
        this.custs = custs;
    }

    public Warehouse(){
        this.dishes.add(new Dish(this.dishes.size()+1,"宫保鸡丁",9.0,"不辣还好吃",0));
        this.dishes.add(new Dish(this.dishes.size()+1,"彪哥小面",8.0,"加豆干好吃",0));
        this.dishes.add(new Dish(this.dishes.size()+1,"杂粮煎饼",7.0,"加肉松好吃",0));
    }
    //订单方法**********************************************************************
    //打印订单<20
    public void printOrder(){
        if(orders.size()>20){
            for (int i = 0; i < 20; i++) {
                System.out.println(orders.get(i));
            }
        }
        System.out.println(orders);
    }
    //将订单存入集合
    public void addOrder(Order order) {
        orders.add(order);
    }
    //通过编号判断是否存在该订单
    public boolean xOrder(String code){
        for (int i = 0; i < orders.size(); i++) {
            if(orders.get(i).getCode().equals(code)){
                return true;
            }
        }
        return false;
    }
    //通过单号返回订单
    public Order findOrder(String code){
        for (int i = 0; i < orders.size(); i++) {
            if(orders.get(i).getCode().equals(code)){
                return orders.get(i);
            }
        }
        return null;
    }
    //订单方法**********************************************************************
    //展示菜单
//    public Dish getDish(int pos){
//        return this.dishes.get(pos);
//    }
    public void show(){
        System.out.println("----------食品列表---------");
        for (int i = 0; i < dishes.size(); i++) {
            System.out.println(this.dishes.get(i));
        }
        System.out.println("----------食品列表---------");
        System.out.println("   共计"+dishes.size()+"条数据");
    }
    //增加菜品库存
    public void addDish(Dish dish) {
        dishes.add(dish);
    }
    //炒菜
    public void cookDish() {
        show();
        System.out.println("----------新增库存----------");
        System.out.println("你想炒的菜名是:");
        String name=scanner.nextLine();
        while (!nameBool(name)){
            System.out.println("请重新输入");
            name=scanner.nextLine();
        }
        String num;
        int nums=0;
        if (nameBool(name)){
            System.out.println("请输入要炒的数量");
             num=scanner.nextLine();
            while (!isNum2(num)){
                if(isDouble(num)){
                    System.out.println("不可以输入小数，请重新输入");
                }else {
                    System.out.println("输入的不是数字，请重新输入");
                }
             num=scanner.nextLine();
            }
             nums=Integer.parseInt(num);
        }
        for (Dish dish : dishes) {
            if (dish.name.equals(name)) {
                dish.stock=dish.stock+nums;
                System.out.println("库存增加成功！");
                return;
            }
        }
    }
    //扔菜
public void throwDish() {
        show();
        boolean pos=true;
        while (pos){
    System.out.println("请输入要丢弃的菜名");
    String name=scanner.nextLine();
    int nums=0;
    for (Dish dish : dishes) {
        if (dish.name.equals(name)) {
        System.out.println("请输入要丢弃的数量");
        String num=scanner.nextLine();
            while (!isNum2(num)){
                if(isDouble(num)){
                    System.out.println("不可以输入小数，请重新输入");
                }else {
                    System.out.println("输入的不是数字，请重新输入");
                }
                num=scanner.nextLine();
            }
        nums=Integer.parseInt(num);
            if (dish.stock >= nums) {
                dish.stock=dish.stock-nums;
                System.out.println("丢失成功！");
            } else {
                System.out.println("库存不足！");
            }
            return;
        }
    }
    System.out.println("菜品不存在！请再次输入");
}
    }
    //修改菜单
    public void setDishes(){
        show();
        System.out.println("请输入要修改的食品id");
        int num;
        while (true){
            String nums=scanner.nextLine();
            while (!isNum2(nums)){
                if(isDouble(nums)){
                    System.out.println("不可以输入小数，请重新输入");
                }else {
                    System.out.println("输入错误，请重新输入");
                }
                nums=scanner.nextLine();
            }
            num=Integer.parseInt(nums);
            if(num<=0||num>dishes.size()){
                System.out.println("输入的id不存在请再次输入");
            }else {
                break;
            }
        }
        Dish dish = dishes.get(num - 1);
        System.out.println("------修改食品-------");
        System.out.println("请输入菜品名称:");
        String name = scanner.nextLine();
        dish.setName(name);
        System.out.println("请输入食品价格:");
        String pr=scanner.nextLine();
        while (!isDouble(pr)){
            System.out.println("输入错误请重新输入");
            pr=scanner.nextLine();
        }
        Double pre=Double.parseDouble(pr);
        dish.setPrice(pre);
        System.out.println("请输入描述:");
        dish.setDesc(scanner.nextLine());
        System.out.println(name+"修改成功");
        dish.setStock(0);
    }
    //根据菜名判断是否存在菜
    public boolean nameBool(String name){
        for (int i=0;i<dishes.size();i++) {
            if (dishes.get(i).name.equals(name)) {
                return true;
            }
        }
        return false;
    }
    //通过菜名返回菜
    public Dish nameDish(String name){
        for (Dish dish : dishes) {
            if (dish.name.equals(name)) {
                return dish;
            }
        }
        return null;
    }
    //根据菜名返回菜下标
    public int xDish(String name){
        for (int i = 0; i < dishes.size(); i++) {
            if(dishes.get(i).name.equals(name)){
                return i;
            }
        }
        return -1;
    }
    //判断是否是double类型的
    public boolean isDouble(String input){
        while (true) {
            double choice;
            try {
                choice = Double.parseDouble(input);
                if (choice > 0 && choice < 2000000) {
                    return true;
                }
            } catch (NumberFormatException e) {
                return false;
            }
            return false;
        }
    }
    //判断是否是整形
    public boolean isNum(String input){
        while (true) {
            int choice;
            try {
                choice = Integer.parseInt(input);
                if (choice > 0 ) {
                    return true;
                } else {
                    return false;
                }
            } catch (NumberFormatException e) {
                System.out.println("输入的不是数字，请重新输入！");
                return false;
            }
        }
    }
    public boolean isNum2(String input){
        while (true) {
            int choice;
            try {
                choice = Integer.parseInt(input);
                if (choice >= 0 ) {
                    return true;
                } else {
                    return false;
                }
            } catch (NumberFormatException e) {
                return false;
            }
        }
    }
    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }
}

